//
//  EditShopNameCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/14.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "EditShopNameCell.h"

@interface EditShopNameCell()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;


@end


@implementation EditShopNameCell

#pragma mark - lifecycle methods

- (void)setupUI
{
    [self addSubview:self.titleLab];
    [self addSubview:self.textfeild];
}

- (void)setupLayout
{
    _titleLab.size = CGSizeMake(100, 20);
    _titleLab.mj_x = 15;
    _titleLab.mj_y = 20;
    
    
    _textfeild.size = CGSizeMake(KScreenWidth - 130, 60);
    _textfeild.mj_x = 115;
    _textfeild.mj_y = 0;
    
    CAShapeLayer *line = [CAShapeLayer layer];
    line.fillColor = mIconLineColor.CGColor;
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 15, 1, 30)];
    line.path = path.CGPath;
    [self.layer addSublayer:line];
}

#pragma mark - delegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger strLength = textField.text.length - range.length + string.length;
    if (strLength > 10){
        
        if (self.wordPromptBlock) {
            self.wordPromptBlock();
        }
        return NO;
    }
    NSString *text = nil;
    //如果string为空，表示删除
    if (string.length > 0) {
        text = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }else{
        text = [textField.text substringToIndex:range.location];
    }
    
    if (text.length > 10) {
        
        return NO;
    }
    
    return YES;
}



#pragma mark - setter methods
#pragma mark - getter methods

- (UILabel *)titleLab{
    
    if (!_titleLab) {
        _titleLab = [UILabel makeLabelWithText:@"店铺名称" textColor:UIColorFromRGB(0x262626) font:mFont(16) textAliment:0];
    }
    return _titleLab;
}


- (UITextField *)textfeild{
    
    if (!_textfeild) {
        _textfeild = [[UITextField alloc]init];
        _textfeild.placeholder = @"请输入10个字以内店铺名称";
        _textfeild.textColor = UIColorFromRGB(0x363636);
        _textfeild.font = mFont(14);
        _textfeild.keyboardType = UIKeyboardTypeDefault;
        _textfeild.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textfeild.returnKeyType = UIReturnKeyDone;
        _textfeild.delegate = self;
        _textfeild.tag = 200;
    }
    return _textfeild;
}

@end
